package cn.jy.ad;

import cn.jy.ad.utils.DatabaseCheckJob;
import com.ruoyi.common.security.annotation.EnableCustomConfig;
import com.ruoyi.common.security.annotation.EnableRyFeignClients;
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2;
import org.mybatis.spring.annotation.MapperScan;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
@EnableCustomConfig
@EnableCustomSwagger2
@EnableRyFeignClients
@MapperScan("cn.jy.ad.mapper")
public class AdApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdApplication.class, args);
        System.out.println("══════════════════════════════════════");
        System.out.println("✨  广告投放 程序启动成功！  ✨");
        System.out.println("══════════════════════════════════════");

        try {
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            Scheduler scheduler = schedulerFactory.getScheduler();

            // 创建任务详情，指向定时任务类
            JobDetail job = JobBuilder.newJob(DatabaseCheckJob.class)
                    .withIdentity("databaseCheckJob", "group1")
                    .build();

            // 设置触发时间，这里设置立即执行并每小时执行一次
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("databaseCheckTrigger", "group1")
                    .startNow()  // 立即执行
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                            .withIntervalInHours(1) // 每小时执行一次
                            .repeatForever()) // 持续重复
                    .build();

            // 将任务和触发关联到调度器
            scheduler.scheduleJob(job, trigger);
            // 启动调度器
            scheduler.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
